package com.woniu.product.controller;

import cn.hutool.core.util.ObjectUtil;
import com.woniu.common.dto.*;
import com.woniu.common.utils.JWTUtil;
import com.woniu.common.utils.PageParam;
import com.woniu.common.utils.ResponseData;
import com.woniu.product.dto.ProductAddDTO;
import com.woniu.product.feign.EcssFeign;
import com.woniu.product.pojo.Coupon;
import com.woniu.product.pojo.ShoppingCart;
import com.woniu.product.pojo.ZyProduct;
import com.woniu.product.service.CouponService;
import com.woniu.product.service.ShoppingCartService;
import com.woniu.product.service.ZyProductService;
import com.woniu.product.service.ZyProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ltyipx
 * @version 1.0
 * @description:TODO
 * @date 2024/8/6 14:17
 */
@RestController
public class ProductController {
    @Autowired
    private ZyProductService productService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private EcssFeign ecssFeign;


    @PostMapping("/selectProduct")
    public List<ZyProduct> selectProduct(@RequestBody String productId) {
        List<ZyProduct> product = productService.selectProduct(productId);
        return product;
    }

    @GetMapping("/queryGoods")
    public ResponseData queryGoods() {
        List<ZyProduct> list = productService.lambdaQuery().eq(ZyProduct::getStatus,2).list();
        return new ResponseData<>().success(list);
    }
    @GetMapping("/queryProductDetail")
    public ResponseData queryProductDetail(String id) {
        ZyProduct product = null;
        try {
            product = productService.getById(id);
            Cookie[] cookies = httpServletRequest.getCookies();
            String jwt = null;
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("jwt")) {
                        jwt = cookie.getValue();
                    }
                }
            }
            Map<String, Object> userInfo = JWTUtil.getUserInfo(jwt);
            ShoppingCart shoppingCart = shoppingCartService.lambdaQuery().eq(ShoppingCart::getUserId, userInfo.get("id").toString())
                    .eq(ShoppingCart::getProductId, id)
                    .one();

            HashMap<String, Object> hashMap = new HashMap<>(2);
            hashMap.put("product", product);
            if (!ObjectUtil.isEmpty(shoppingCart)) {
                hashMap.put("buyNum", shoppingCart.getBuyNum());
            }

            return new ResponseData().success(hashMap);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/queryCoupon")
    public ResponseData queryCoupon(String id) {
        List<Coupon> list = couponService.queryCoupon(id);
        return new ResponseData().success(list);

    }

    @GetMapping("/getCoupon")
    public ResponseData getCoupon(String couponId) {
        return couponService.getCoupon(couponId);
    }

    @PostMapping("/queryAllProduct")
    public ResponseData<?> queryProduct(@RequestBody PageParam<ProductDto> params) {
        return productService.queryProduct(params);
    }

    @GetMapping("/queryGoodsByIds")
    public ResponseData queryGoodsByIds(String[] ids) {
        List<ZyProduct> list = productService.lambdaQuery().in(ZyProduct::getId, ids).list();
        return new ResponseData().success(list);
    }

    @PostMapping("/xiaJia")
    public ResponseData<?> xiaJia(@RequestBody List<String> productId) {
        ecssFeign.TBXiaJia(productId);
        return productService.xiaJia(productId);
    }

    @PostMapping("/shangJia")
    public ResponseData<?> shangJia(@RequestBody List<String> productId) {
        ecssFeign.TBShangJia(productId);
        return productService.shangJia(productId);
    }

    @PostMapping("/updateProductType")
    public ResponseData<?> updateProductType(@RequestBody XGFLDto XGFL) {
        ecssFeign.TBUpdateProductType(XGFL);
        return productService.updateProductType(XGFL);
    }

    @PostMapping("/addProduct")
    public ResponseData<?> addProduct(@RequestBody ZyProductDto form) {
        try {
            return productService.addProduct(form);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/rollBackStock")
    public void rollBackStock(@RequestBody List<OrderDetailDTO> list) {
        productService.rollBackStock(list);
    }


    @PostMapping("/productPush")
    public Boolean productPush(@RequestBody List<ProductAddDTO> productAddDTOList) {
        try {
            return productService.productPush(productAddDTOList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 售后成功时商品库存回滚
     * @param shouhouProductDto
     * @return
     */
    @PostMapping("/saveProduct")
    public boolean saveProduct(@RequestBody ShouhouProductDto shouhouProductDto){
        String productId = shouhouProductDto.getId();
        ZyProduct product = productService.getById(productId);
        product.setStock(shouhouProductDto.getStock()+product.getStock());;
        boolean save = productService.updateById(product);
        return save;
    }

    @PostMapping("/updateProductSales")
    ResponseData updateProductSales(@RequestBody ArrayList<ForOrderProductDTO> forOrderProductDTOS){
        return productService.updateProductSales(forOrderProductDTOS);
    }

    @PostMapping("/updateCart")
    ResponseData updateCart(@RequestParam("userId") String userId, @RequestBody List<ForOrderProductDTO> products){
        return shoppingCartService.updateCart(userId,products);
    }
    @GetMapping("/modifyProduct")
    public ResponseData<?> modifyProduct(@RequestParam String id){
        return productService.modifyProduct(id);
    }
    @PostMapping("/deleteProduct")
    public ResponseData<?> deleteProduct(@RequestBody List<String> productId){
        ecssFeign.TBDeletePro(productId);
        return productService.deleteProduct(productId);
    }
}